package org.mili.ticketeer.ui.contact;

import org.mili.ticketeer.dto.Contact;
import org.mili.ticketeer.dto.Identifieable;
import org.mili.ticketeer.dto.SexEnum;
import org.mili.ticketeer.service.ContactService;
import org.mili.ticketeer.service.ServiceException;
import org.mili.ticketeer.service.ServiceFactory;
import org.mili.ticketeer.ui.FormElement;
import org.mili.ticketeer.ui.SimpleForm;
import org.mili.ticketeer.util.Log;

import javax.swing.*;

/**
 * Created with IntelliJ IDEA.
 * User: papa
 * Date: 20.07.13
 * Time: 14:10
 * To change this template use File | Settings | File Templates.
 */
public class ContactDataForm extends SimpleForm<Contact> {
    private ContactService contactService = ServiceFactory.getService(ContactService.class);

    public ContactDataForm(String label) {
        super(FormMode.WIDE, label, new FormElement[]{
                new FormElement(Identifieable.Fields.ID, new JTextField()),
                new FormElement(Contact.Fields.FIRSTNAME, new JTextField()),
                new FormElement(Contact.Fields.LASTNAME, new JTextField()),
                new FormElement(Contact.Fields.STREET, new JTextField()),
                new FormElement(Contact.Fields.NUMBER, new JTextField()),
                new FormElement(Contact.Fields.ZIP, new JTextField()),
                new FormElement(Contact.Fields.PHONE, new JTextField()),
                new FormElement(Contact.Fields.EMAIL, new JTextField()),
                new FormElement(Contact.Fields.TOKEN, new JTextField()),
                new FormElement(Contact.Fields.ISSPEAKER, new JCheckBox()),
                new FormElement(Contact.Fields.ACTIVE, new JCheckBox()),
                new FormElement(Contact.Fields.SEX, new JComboBox<SexEnum>())
        });

        JTextField id = (JTextField) getFormElement(Identifieable.Fields.ID).getComponent();
        id.setEnabled(false);

        JComboBox sex = (JComboBox) getFormElement(Contact.Fields.SEX).getComponent();
        sex.setModel(new DefaultComboBoxModel(SexEnum.values()));
    }

    @Override
    public void save() throws ServiceException {
        Log.log(this, "save", "begin");
        formObject.setEmail(getString(Contact.Fields.EMAIL));
        formObject.setLastName(getString(Contact.Fields.LASTNAME));
        formObject.setFirstName(getString(Contact.Fields.FIRSTNAME));
        formObject.setStreet(getString(Contact.Fields.STREET));
        formObject.setNumber(getString(Contact.Fields.NUMBER));
        formObject.setPhone(getString(Contact.Fields.PHONE));
        formObject.setToken(getString(Contact.Fields.TOKEN));
        formObject.setZip(getInt(Contact.Fields.ZIP));
        formObject.setSex((SexEnum) getObject(Contact.Fields.SEX));
        formObject.setSpeaker(getBoolean(Contact.Fields.ISSPEAKER));
        formObject.setActive(getBoolean(Contact.Fields.ACTIVE));
        contactService.write(formObject);
        Log.log(this, "save", "end");
    }

    @Override
    public void reset() {
        Log.log(this, "reset", "begin");
        if (formObject.getId() > 0) {
            onSelectionChanged(formObject);
        } else {
            onSelectionChanged(new Contact());
        }
        Log.log(this, "reset", "end");
    }

    @Override
    public void cancel() {
        Log.log(this, "cancel", "begin");
        reset();
        Log.log(this, "cancel", "end");
    }

    @Override
    public boolean validation() {
        Log.log(this, "validation", "true");
        return true;
    }

    @Override
    public void onSelectionChanged(Object element) {
        Log.log(this, "selectionChanged", "element: %s", element);
        if (element instanceof Contact) {
            formObject = (Contact) element;
            updateTextField(Identifieable.Fields.ID, String.valueOf(formObject.getId()));
            updateTextField(Contact.Fields.LASTNAME, formObject.getLastName());
            updateTextField(Contact.Fields.FIRSTNAME, formObject.getFirstName());
            updateTextField(Contact.Fields.EMAIL, formObject.getEmail());
            updateTextField(Contact.Fields.STREET, formObject.getStreet());
            updateTextField(Contact.Fields.NUMBER, formObject.getNumber());
            updateTextField(Contact.Fields.PHONE, formObject.getPhone());
            updateTextField(Contact.Fields.TOKEN, formObject.getToken());
            updateTextField(Contact.Fields.ZIP, String.valueOf(formObject.getZip()));
            updateComboBox(Contact.Fields.SEX, formObject.getSex());
            updateCheckBox(Contact.Fields.ISSPEAKER, formObject.isSpeaker());
            updateCheckBox(Contact.Fields.ACTIVE, formObject.isActive());
        }
        Log.log(this, "selectionChanged", "end");
    }

}
